<template>
	<view>
		<view class="u-p-r" style="z-index: 2;">
			<u-navbar title="登录" :auto-back="true" @leftClick="back" placeholder bg-color="transparent" titleStyle="color:#fff">
				
			</u-navbar>
			<u-gap height="30rpx"></u-gap>
			<view style="padding: 52rpx;">
				<view class="u-flex" style="align-items: flex-end;">
					<view class="u-f-48 u-bold u-m-r-8">
						您好，<br/>
						欢迎使用
					</view>
					
				</view>
			</view>
			<u-gap height="67rpx"></u-gap>
			<view style="margin-top: -30rpx;" class="u-p-30 u-r-30 u-bg-fff">
				
				<button open-type="getPhoneNumber" :plain="true" class="appBtn wx" @getphonenumber="getPhone">手机号快捷登录</button>
				<button :plain="false" class="appBtn back" @click="back">返回首页</button>
	
				
			</view>
			
		</view>
	
		<view class="u-text-center u-f-24 u-m-t-24">
							
							<checkbox style="transform: scale(0.8);" :checked="isTrue" @click="isTrue=!isTrue"></checkbox>
							请阅读协议<text style="color: #EA4D44;"
								 @click="$u.route('pages/login/agree',{type:'use'})" >《用户协议》</text>和<text
								style="color: #EA4D44;"
								 @click="$u.route('pages/login/agree',{type:'privacy'})" >《隐私政策》</text>
						</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				tips: '',
				// refCode: null,
				seconds: 60,
				loginFrom: {
					username: null,
					smscode: null
				},
				isTrue:false,
				openid:'',
				session_key:'',
				unionid:'',
				platform:''
			}
		},
		onShow() {
			
			let systemInfo = uni.getSystemInfoSync();
			this.platform = systemInfo.platform
			// #ifdef MP-WEIXIN
				let _this = this
				uni.login({
				  provider: 'weixin', //使用微信登录
				  success: e => {
					let code = e.code;
					console.log(code)
					uni.$u.http.post('Login/getOpenid', {
						code: code,
						
					}).then(res => {
						_this.openid = res.openid
						_this.session_key = res.session_key
						_this.unionid = res.unionid
					})
				  }
				});
			// #endif
		},
		methods: {
			back(){
				uni.switchTab({
					url: "/pages/index/index"
				})
			},
			wxLogin(){
				if(this.isTrue == false){
					return uni.$u.toast('请阅读协议');
				}
				uni.login({
					"provider": "weixin",
					// "onlyAuthorize": true, // 微信登录仅请求授权认证
					success: function(event){
						const {code} = event
						 uni.getUserInfo({
						      provider: 'weixin',
						      success: async function (infoRes) {
								let res = await uni.$u.http.post('/login/wxcode', infoRes)
								console.log(res)
								if(res.mobile){
									uni.setStorageSync('token', res.token)
									uni.setStorageSync('userInfo', res)
									uni.switchTab({
										url:'/pages/member/member'
									})
								}else{
									uni.setStorageSync('wechat',infoRes.userInfo)
									uni.navigateTo({
										url:'/pages/login/weChat'
									})
								}
						      }
						    });
						
						
					},
					fail: function (err) {
						
						uni.$u.http.post('/login/wxcode', err)
				       uni.$u.toast('授权失败');
				    }
				})
			},
			codeChange(text) {
				this.tips = text;
			},
			async getCode() {
				if(this.isTrue == false){
					return uni.$u.toast('请阅读协议');
				}
				if (this.$refs.uCode.canGetCode) {
					// 模拟向后端请求验证码
					uni.showLoading({
						title: '正在获取验证码'
					})
					// setTimeout(() => {
						
					let param = {
						mobile:this.loginFrom.username,
						type:'login'
					}
						
					await uni.$u.http.post('/sms/sendcode', param)
						
					uni.hideLoading();
					// 这里此提示会被this.start()方法中的提示覆盖
					uni.$u.toast('验证码已发送');
					// 通知验证码组件内部开始倒计时
					this.$refs.uCode.start();
						
					// }, 2000);
				} else {
					uni.$u.toast('倒计时结束后再发送');
				}
			},
			async login() {
				if(this.isTrue == false){
					return uni.$u.toast('请阅读协议');
				}
				this.loginFrom.openid=this.openid
				this.loginFrom.unionid=this.unionid
				let res = await uni.$u.http.post('login/login', this.loginFrom)
			
				uni.setStorageSync('userInfo', res)
				uni.setStorageSync('token', res.token)
				uni.switchTab({
					url:'/pages/member/member'
				})
			},
			async getPhone(e){
				if(this.isTrue == false){
					return uni.$u.toast('请阅读协议');
				}
				this.phoneData = e.detail
				let param = {
					encryptedData: e.detail.encryptedData,
					iv: e.detail.iv,
					sessionKey: this.session_key,
					openid: this.openid,
					unionid:this.unionid
				}
				let res=await uni.$u.http.post('Login/mplogin',param)
				if(res.mobile){
					uni.setStorageSync('token', res.token)
					uni.setStorageSync('userInfo', res)
					uni.switchTab({
						url:'/pages/member/member'
					})
				}else{
					uni.setStorageSync('wechat',res)
					uni.navigateTo({
						url:'/pages/login/weChat'
					})
				}
				
				
				
			},
			end() {
				uni.$u.toast('倒计时结束');
			},
			start() {
				// uni.$u.toast('倒计时开始');
			}
		}
	}
</script>

<style lang="scss">
		.inp{
			padding-top: 66rpx;
			padding-bottom: 40rpx;
			border-bottom: 1px solid #e2e2e2;
			display: flex;
			align-items: center;
		}
		.appBtn {
			&.back {
				padding: 0;
				border: 1rpx solid #EA4D44;
				color: #EA4D44;
				background: #fff !important;
				margin-top: 40rpx;
			}
			&.wx {
				margin: 20rpx 0 0;
				border: none;
				color: #fff;
			}
			
		}
</style>
